home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C17 / SiteMapConvert.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.3 KB  |  82 lines

  1. //: C17:SiteMapConvert.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Using strings to create a custom conversion
  7. // program that generates HTML output
  8. #include "../require.h"
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <cstdlib>
  13. using namespace std;
  14.  
  15. class Item {
  16.   string id, url;
  17.   int depth;
  18.   string removeBar(string s) {
  19.     if(s[0] == '|')
  20.       return s.substr(1);
  21.     else return s;
  22.   }
  23. public:
  24.   Item(string in, int& index) : depth(0) {
  25.     while(in[index] == '#' && index < in.size()){
  26.       depth++;
  27.       index++;
  28.     }
  29.     // 0 means no '#' marks were found:
  30.     if(depth == 0) depth = 1;
  31.     while(in[index] != '%' && index < in.size())
  32.       id += in[index++];
  33.     id = removeBar(id);
  34.     index++; // Move past '%'
  35.     while(in[index] != '*' && index < in.size())
  36.       url += in[index++];
  37.     url = removeBar(url);
  38.     index++; // To move past '*'
  39.   }
  40.   string identifier() { return id; }
  41.   string path() { return url; }
  42.   int level() { return depth; }
  43. };
  44.  
  45. int main(int argc, char* argv[]) {
  46.   requireArgs(argc, 1,
  47.     "usage: SiteMapConvert inputfilename");
  48.   ifstream in(argv[1]);
  49.   assure(in, argv[1]);
  50.   ofstream out("plainmap.html");
  51.   string line;
  52.   while(getline(in, line)) {
  53.     if(line.find("<param name=\"source_file\"")
  54.        != string::npos) {
  55.       // Extract data of from start of sequence
  56.       // until the terminating quote mark:
  57.       line = line.substr(line.find("value=\"") 
  58.              + string("value=\"").size());
  59.       line = line.substr(0, 
  60.                line.find_last_of("\""));
  61.       int index = 0;
  62.       while(index < line.size()) {
  63.         Item item(line, index);
  64.         string startLevel, endLevel;
  65.         if(item.level() == 1) {
  66.           startLevel = "<h1>";
  67.           endLevel = "</h1>";
  68.         } else
  69.           for(int i = 0; i < item.level(); i++)
  70.             for(int j = 0; j < 5; j++)
  71.               out << " ";
  72.         string htmlLine = "<a href=\""
  73.           + item.path() + "\">"
  74.           + item.identifier() + "</a><br>";
  75.         out << startLevel << htmlLine 
  76.             << endLevel << endl;
  77.       }
  78.       break; // Out of while loop
  79.     }
  80.   }
  81. } ///:~
  82.